package com.lune.job.controller;

import com.lune.job.framework.redis.RedisUtil;
import com.lune.job.framework.role.Role;
import com.lune.job.pojo.Company;
import com.lune.job.pojo.RespBean;
import com.lune.job.pojo.Student;
import com.lune.job.pojo.User;
import com.lune.job.service.ICompanyService;
import com.lune.job.service.IStudentService;
import com.lune.job.service.IUserService;
import com.lune.job.service.impl.UserServiceImpl;
import com.lune.job.vo.UserData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 登录
 **/
@RestController
@Api(tags = "登录")
public class LoginController {

    @Autowired
    private IUserService userService;
    @Autowired
    private ICompanyService companyService;
    @Autowired
    private IStudentService studentService;
    @Autowired
    private RedisUtil redisUtil;
    @ApiOperation(value = "登录")
    @PostMapping("/login")
    public RespBean login(@RequestBody Map<String,String> map){
        String account=map.get("account");
        String password=map.get("password");
        String type=map.get("type");
        boolean flag=false;
        UserData userData = new UserData();
        if (String.valueOf(Role.ADMIN.getCode()).equals(type)){
            //管理员登录
            User login = userService.login(account, password);
            if (login!=null){
                userData.setId(login.getId());
                userData.setAccount(login.getUserName());
                userData.setName(login.getName());
                userData.setType(Role.ADMIN.getCode()); //指定
                flag=true;
            }
        }
        if (String.valueOf( Role.COMPANY.getCode()).equals(type)){
            //招聘单位登录
            Company login = companyService.login(account, password);
            if (login!=null){
                userData.setId(login.getId());
                userData.setAccount(login.getAccount());
                userData.setName(login.getName());
                userData.setType(Role.COMPANY.getCode()); //指定
                flag=true;
            }
        }
        if (String.valueOf(Role.STUDENT.getCode()).equals(type)){
            //学生登录
            Student login = studentService.login(account, password);
            if (login!=null){
                userData.setId(login.getId());
                userData.setAccount(login.getAccount());
                userData.setName(login.getName());
                userData.setType(Role.STUDENT.getCode()); //指定
                flag=true;
            }
        }
        if (flag){//登录成功的情况
            //生成token
            String token = UUID.randomUUID().toString();
            userData.setToken(token);
            redisUtil.set(token,userData,redisUtil.EXPR);
            return  RespBean.success("登录成功",userData);
        }else{
            return RespBean.error("用户或者密码错误");
        }
    }

}
